﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CTEM.Controllers.BUS;
using CTEM.Models;


namespace CTEM.ModuleForms.ManageRole
{
    public partial class DXRoleManagement : DevExpress.XtraEditors.XtraForm
    {
        RoleBUS _roleBus = new RoleBUS();
        List<RoleDTO> _listRole = new List<RoleDTO>();
        List<RoleDTO> listRoleHistory = new List<RoleDTO>();

        public DXRoleManagement()
        {
            InitializeComponent();
            loadRole();            
        }

        private void loadRole()
        {
            _listRole = _roleBus.GetWithoutAdmin();
            roleDTOBindingSource.DataSource = _listRole;
        }

        private static RoleDTO findRoleByID(List<RoleDTO> listRole, long RoleID)
        {
            RoleDTO role = new RoleDTO();
            foreach (RoleDTO r in listRole)
            {
                if (r.RoleID == RoleID)
                    role = r;
                break;
            }
            return role;
        }

        private void gridView1_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            var newRole = e.Row as RoleDTO;
            if (newRole.RoleID == 0)
            {
                bool success = _roleBus.AddRole(newRole);
                if (success)
                {
                    loadRole();
                    newRole.Status = 1;
                    listRoleHistory.Add(newRole);
                }
            }
            else
            {
                bool success = _roleBus.EditRole(newRole);
                if (success)
                {
                    loadRole();
                    newRole = findRoleByID(_listRole, newRole.RoleID);
                    newRole.Status = 2;
                    listRoleHistory.Add(newRole);
                }
            }
        }

        private void BntDeleteRoleClick(object sender, EventArgs e)
        {
            int rowIndex = gridView1.FocusedRowHandle;
            var role = gridView1.GetRow(rowIndex) as RoleDTO;
            if (MessageBox.Show("Bạn có chắc chắn muốn xóa chức vụ "+ role.RoleName +" không?", "Cảnh báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                    bool success = _roleBus.DeleteRole(role.RoleID);
                    if (success)
                    {
                        loadRole();
                        role.Status = 3;
                        listRoleHistory.Add(role);
                    }
                    else
                    MessageBox.Show("Bạn phải xóa những nhân viên đang giữ chức vụ này trước.","Cảnh báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }

        private void btnSetRights_Click(object sender, EventArgs e)
        {
            int rowIndex = gridView1.FocusedRowHandle;
            if (rowIndex != null)
            {
                var role = gridView1.GetRow(rowIndex) as RoleDTO;
                var setRightForm = new DXSetRight(role.RoleID, role.RightsValue, _listRole);
                setRightForm.ShowDialog();
            }
            else
            {
                DXSetRight setRightForm = new DXSetRight(_listRole);
                setRightForm.ShowDialog();
            }
        }

    }
}
